//
//  AddRepaymentView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/10/10.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol AddRepaymentViewDelegate {
    
    @objc optional func selectOperte(index:Int) //选择操作
}

class AddRepaymentView: UIView {
    
    weak var delegate : AddRepaymentViewDelegate?
    
    let numberView = AddNewsView()
    let bankView = SelectNewsView()
    let dayView = SelectNewsView()
    let dateView = SelectNewsView()
    let phoneView = AddNewsView()
    let remarksView = AddNewsView()
    
    let addButton = UIButton()
    
    init() {
        super.init(frame: .zero)
        self.backgroundColor = UIColor.verifyColor()
        
        let titleLabel = UILabel()
        titleLabel.textAlignment = .center
        titleLabel.text = addRepayment
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        self.addSubview(titleLabel)
        
        numberView.textView.keyboardType = .numberPad
        numberView.text = afterBankNumber
        numberView.placeholder = bankNumberTips
        self.addSubview(numberView)
        
        bankView.selectBtn.tag = 1
        bankView.title = bankOf
        bankView.text = BankofChina
        bankView.selectBtn.addTarget(self, action: #selector(selectOperte(sender:)), for: .touchUpInside)
        self.addSubview(bankView)
        
        dayView.selectBtn.tag = 2
        dayView.title = daysInAdvance
        dayView.text = "1" + days
        dayView.selectBtn.addTarget(self, action: #selector(selectOperte(sender:)), for: .touchUpInside)
        self.addSubview(dayView)
        
        dateView.selectBtn.tag = 3
        dateView.title = repaymentDate
        dateView.text = "1" + number
        dateView.selectBtn.addTarget(self, action: #selector(selectOperte(sender:)), for: .touchUpInside)
        self.addSubview(dateView)
        
        phoneView.textView.keyboardType = .numberPad
        phoneView.text = sendToPhone
        phoneView.placeholder = phoneNumberTips
        self.addSubview(phoneView)
        
        remarksView.text = remarks
        remarksView.placeholder = remarksTips
        self.addSubview(remarksView)
        
        addButton.tag = 4
        addButton.setTitle(add_title, for: .normal)
        addButton.backgroundColor = UIColor.red
        addButton.layer.cornerRadius = RATIO_H(maxNum: 20)
        addButton.layer.masksToBounds = true
        addButton.addTarget(self, action: #selector(selectOperte(sender:)), for: .touchUpInside)
        self.addSubview(addButton)
        
        titleLabel.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 40))
        }
        
        numberView.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 50))
        }
        
        bankView.snp.makeConstraints { (make) in
            make.top.equalTo(numberView.snp.bottom).offset(1)
            make.left.right.equalToSuperview()
            make.height.equalTo(numberView)
        }
        
        dayView.snp.makeConstraints { (make) in
            make.top.equalTo(bankView.snp.bottom).offset(1)
            make.left.right.equalToSuperview()
            make.height.equalTo(bankView)
        }
        
        dateView.snp.makeConstraints { (make) in
            make.top.equalTo(dayView.snp.bottom).offset(1)
            make.left.right.equalToSuperview()
            make.height.equalTo(dayView)
        }
        
        phoneView.snp.makeConstraints { (make) in
            make.top.equalTo(dateView.snp.bottom).offset(1)
            make.left.right.equalToSuperview()
            make.height.equalTo(dateView)
        }
        
        remarksView.snp.makeConstraints { (make) in
            make.top.equalTo(phoneView.snp.bottom).offset(1)
            make.left.right.equalToSuperview()
            make.height.equalTo(phoneView)
        }
        
        addButton.snp.makeConstraints { (make) in
            make.top.equalTo(remarksView.snp.bottom).offset(RATIO_H(maxNum: 20))
            make.left.equalToSuperview().offset(RATIO(maxNum: 20))
            make.right.equalToSuperview().offset(RATIO(maxNum: -20))
            make.height.equalTo(RATIO_H(maxNum: 40))
        }
    }
    
    //MARK: 选择操作类型
    @objc fileprivate func selectOperte(sender:UIButton) {
        
        self.delegate?.selectOperte!(index: sender.tag)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
